package com.qf.pay;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.github.wxpay.sdk.WXPayRequest;
import com.github.wxpay.sdk.WXPayUtil;
import com.qf.pay.config.WXConfig;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * 测试微信的api
 */
public class PayDemo {


    /**
     * 测试统一下单接口
     */
//    public static void main(String[] args) throws Exception{
//        //步骤：
//        //1:构建配置对象
//        WXConfig wxConfig = new WXConfig();
//
//        //2:封装请求参数
//        Map requestParamMap = new HashMap();
//        requestParamMap.put("appid",wxConfig.getAppID());
//        requestParamMap.put("mch_id",wxConfig.getMchID());
//        requestParamMap.put("nonce_str", WXPayUtil.generateNonceStr());
//        requestParamMap.put("body","购买套餐");
//        String idStr = IdWorker.getIdStr();
//        System.out.println(idStr);
//        requestParamMap.put("out_trade_no", idStr);
//        requestParamMap.put("total_fee","1");
//        requestParamMap.put("notify_url","http://www.baidu.com");
//        requestParamMap.put("trade_type","NATIVE");
//
//
//        //3:map转xml
//        String requestXML = WXPayUtil.generateSignedXml(requestParamMap, wxConfig.getKey());
//
//        //4:发送请求WXPayRequest request = new WXPayRequest(config);
//        WXPayRequest request = new WXPayRequest(wxConfig);
//        String responseXML = request.requestWithoutCert("/pay/unifiedorder", UUID.randomUUID().toString(), requestXML, false);
//        System.out.println("responseXML = " + responseXML);
//        //5:响应结果转map集合
//        Map<String, String> map = WXPayUtil.xmlToMap(responseXML);
//
//        //6:获取支付地址
//        String code_url = map.get("code_url");
//        System.out.println("code_url = " + code_url);
//
//    }

    //微信的查询订单支付状态接口测试
//    public static void main(String[] args) throws Exception{
//        WXConfig wxConfig = new WXConfig();
//
//
//        //请求参数
//        Map requestParamMap = new HashMap();
//        requestParamMap.put("appid",wxConfig.getAppID());
//        requestParamMap.put("mch_id",wxConfig.getMchID());
//        requestParamMap.put("out_trade_no","1718166053235765249");
//        requestParamMap.put("nonce_str",WXPayUtil.generateNonceStr());
//
//
//
//        //转换成xml
//        String requestParamXML = WXPayUtil.generateSignedXml(requestParamMap, wxConfig.getKey());
//
//
//        //发送请求
//        WXPayRequest request = new WXPayRequest(wxConfig);
//
//        String responseXML = request.requestWithoutCert("/pay/orderquery", UUID.randomUUID().toString(),
//                requestParamXML, false);
//
//        System.out.println(responseXML);
//
//        //解析结果
//        Map<String, String> responseMap = WXPayUtil.xmlToMap(responseXML);
//
//        responseMap.forEach((k,v)->{
//            System.out.println("k = " + k);
//            System.out.println("v = " + v);
//        });
//
//    }


    public static void main(String[] args) throws Exception{
        WXConfig wxConfig = new WXConfig();


        //请求参数
        Map requestParamMap = new HashMap();
        requestParamMap.put("appid",wxConfig.getAppID());
        requestParamMap.put("mch_id",wxConfig.getMchID());
        requestParamMap.put("out_trade_no","1718166053235765249");
        requestParamMap.put("nonce_str",WXPayUtil.generateNonceStr());



        //转换成xml
        String requestParamXML = WXPayUtil.generateSignedXml(requestParamMap, wxConfig.getKey());


        //发送请求
        WXPayRequest request = new WXPayRequest(wxConfig);

        String responseXML = request.requestWithoutCert("/pay/closeorder", UUID.randomUUID().toString(),
                requestParamXML, false);

        System.out.println(responseXML);

        //解析结果
        Map<String, String> responseMap = WXPayUtil.xmlToMap(responseXML);

        responseMap.forEach((k,v)->{
            System.out.println("k = " + k);
            System.out.println("v = " + v);
        });

    }

}
